package com.wyk.controller;

import com.netflix.discovery.converters.Auto;
import com.wyk.bean.Payment;
import com.wyk.common.Result;
import com.wyk.lb.LoadBlancer;
import com.wyk.lb.MyLB;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author WangYuKai
 * @Date 2022/5/13
 * @apiNote
 */
@RestController
@Slf4j
public class OrderController {
    //注如接口
    @Autowired
    private DiscoveryClient discoveryClient;
    //注入自定义的轮训算法
    @Autowired
    private LoadBlancer loadBlancer;
    //注入restTemplate
    @Autowired
    private RestTemplate restTemplate;

    //要访问资源的url
    //private final String PAYMENT_UTL = "http://localhost:8001"; //单击访问
    private final String PAYMENT_UTL = "http://CLOUD-ZHIFU-SERVICE"; //配置文件访问

    //访问提供者的端口号
    @GetMapping("/consumer/payment/create")
    public Result<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_UTL + "/payment/create", payment, Result.class);
    }

    //根据id查询，调用提供这的方法
    @GetMapping("/consumer/payment/{id}")
    public Result getPaymentById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_UTL + "/payment/" + id, Result.class);

    }

    //获取服务发现的信息
    @GetMapping("/consumer/payment/discovery")
    public Object discovery() {
        return restTemplate.getForObject(PAYMENT_UTL + "/payment/discovery", Object.class);
    }

    /**
     * 使用restTemplate进行不同的访问
     * 分别使用getForObject和getForEntity
     */
    @GetMapping("/consumer/payment2/{id}")
    public Result getPayMent2(@PathVariable Integer id) {
        ResponseEntity<Result> forEntity = restTemplate.getForEntity(PAYMENT_UTL + "/payment/" + id, Result.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return new Result(200, "查询成功了", forEntity);
        } else {
            return new Result(444, "查询失败");
        }
    }

    @PostMapping("/consumer/payment/create2")
    public Result create2(Payment payment) {
        return restTemplate.postForObject(PAYMENT_UTL + "/payment/create", payment, Result.class);
    }

    //调用服务端的接口
    @GetMapping("/consumer/payment/lb")
    public String getServers(){
        //获取所有服务
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ZHIFU-SERVICE");
        if(instances == null || instances.size()<= 0){
            return null;
        }
        ServiceInstance instance = loadBlancer.instance(instances);
        //获取到访问的地址
        URI uri = instance.getUri();
        //访问对应的地址
        String forObject = restTemplate.getForObject(uri + "/payment/lb", String.class);
        return forObject;
    }
}
